package com.incms.web.common.controller;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.incms.web.common.shiro.Principal;


/**
 *     
 * 项目名称：inCms    
 * 类名称：BaseController    
 * 类描述：控制层基类    
 * 创建人：liujunqing    
 * 创建时间：2015年10月15日  
 * @version 1.0    
 *
 */
@Slf4j
public class BaseController {
	public static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	/**
	 * 瞬时消息名称
	 */
	public static final String FLASH_MESSAGE_REDIRECT_ATTRIBUTES = "FLASH_MESSAGE";
	
	
	public enum MessageTypeEnum{
		WARN(0),		//警告
		SUCCESS(1),		//成功
		ERROR(2);		//失败
		private Integer key;
		private MessageTypeEnum(Integer key){
			this.key = key;
		}
		public Integer getKey() {
			return this.key;
		}
		public void setKey(Integer key) {
			this.key = key;
		}
		
		
	}
	
	/**
	 * 获得登录身份信息
	 *@return
	 */
	public Principal getPrincipal(){
		return (Principal)SecurityUtils.getSubject().getPrincipal();
	}
	/**
	 * 加入瞬时消息
	 *@param redirectAttributes 重定向对象
	 *@param type				类型@see MessageTypeEnum
	 *@param content			瞬时内容
	 */
	public void addFlashMessage(RedirectAttributes redirectAttributes,MessageTypeEnum type,String content){
		Map<String, String> mesMap = new HashMap<String, String>();
		mesMap.put("type", type.getKey().toString());
		mesMap.put("content", content);
		redirectAttributes.addFlashAttribute(FLASH_MESSAGE_REDIRECT_ATTRIBUTES,gson.toJson(mesMap));
	}
	
	/**
	 * 初始化数据绑定
	 */
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder){
		/**
		 * 绑定日期类型
		 */
		binder.registerCustomEditor(Date.class,new PropertyEditorSupport(){
			private SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
			
			@Override
			public String getAsText() {
				Date date = (Date)getValue();
				return sdf.format(date);
			}
			
			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				if(StringUtils.isBlank(text)){
					setAsText(null);
				}else{
					try {
						DateUtils.parseDate(text, new String[] { "yyyy", "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyy-MM-dd", "yyyyMMdd", "yyyy/MM/dd", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss" });
					} catch (ParseException e) {
						setAsText(null);
						log.error(e.getMessage(),e);
					}
				}
			}
			
		});
		//绑定字符类型然后转义，防止xss攻击
		binder.registerCustomEditor(String.class, new PropertyEditorSupport(){
			@Override
			public String getAsText() {
				String value = (String)getValue();
				return value != null ? value.toString() : "";
			}
			
			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				setValue(text == null ? null : StringEscapeUtils.escapeHtml(text));
			}
		});
		
	}
	
	
}
